const path = require("path");
const execSync = require("child_process").execSync;
try {
  let gitForbidden = require("../../package.json").gitForbidden;
  if (!gitForbidden || gitForbidden.length === 0) {
    process.exit(0);
  }
  gitForbidden = gitForbidden.map((item) => path.resolve(item));
  const result = execSync("git status -s", { encoding: "utf-8" });
  const fileList = result.split("\n").filter((item) => {
    return /^M/.test(item);
  }).map((item) => {
    if (/^M  "(.+?)"$/.test(item)) {
      item = item.replace(/^M  "(.+?)"$/, "$1");
    } else {
      item = item.replace(/^M  (.+?)$/, "$1");
    }
    return path.resolve(item);
  });
  for (const file of fileList) {
    if (gitForbidden.includes(file)) {
      execSync(`git reset -q HEAD -- ${file}`);
    }
  }
} catch (error) {
  console.log(error);
  process.exit(1);
}

process.exit(0);